<?php

/**
 * SfdataModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * $
 */
class SfdataModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["id"] = (int)$value["id"];
		$v["name"] = $value["name"];
		$v["ip"] = $value["ip"];
		$v["routeline"] = $value["routeline"];
		$v["sdatetime"] = $value["sdatetime"];
		$v["qq"] = $value["qq"];
		$v["intro"] = $value["intro"];
		$v["url"] = $value["url"];
		$v["ggdate"] = (int)$value["ggdate"];
		$v["isyellow"] = (int)$value["isyellow"];
		$v["isdayfine"] = (int)$value["isdayfine"];
		$v["isfine"] = (int)$value["isfine"];
		$v["fineseq"] = (int)$value["fineseq"];
		$v["isbest"] = (int)$value["isbest"];
		$v["isfinehot"] = (int)$value["isfinehot"];
		$v["ishot"] = (int)$value["ishot"];
		$v["ischeck"] = (int)$value["ischeck"];
		$v["pubtime"] = (int)$value["pubtime"];
		$v["pubip"] = (int)$value["pubip"];
		$v["adminid"] = (int)$value["adminid"];
		
		$this->db->insert("sfdata", $v);
		$this->updateCache();
	}
	
	/**
	 * 修改
	 */
	public function change($id, array $value)
	{
		$v = array();
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["ip"]) && $v["ip"] = $value["ip"];
		isset($value["routeline"]) && $v["routeline"] = $value["routeline"];
		isset($value["sdatetime"]) && $v["sdatetime"] = $value["sdatetime"];
		isset($value["qq"]) && $v["qq"] = $value["qq"];
		isset($value["intro"]) && $v["intro"] = $value["intro"];
		isset($value["url"]) && $v["url"] = $value["url"];
		$v["ggdate"] = (int)$value["ggdate"];
		$v["isyellow"] = (int)$value["isyellow"];
		$v["isdayfine"] = (int)$value["isdayfine"];
		$v["isfine"] = (int)$value["isfine"];
		$v["fineseq"] = (int)$value["fineseq"];
		$v["isbest"] = (int)$value["isbest"];
		$v["isfinehot"] = (int)$value["isfinehot"];
		$v["ishot"] = (int)$value["ishot"];
		$v["ischeck"] = (int)$value["ischeck"];

		$this->db->update("sfdata", $v, 'id=' . (int)$id);
		$this->updateCache();
	}
	
	/**
	 * 删除
	 */
	public function del($id)
	{
		$this->db->delete("sfdata", 'id=' .  (int)$id);
		$this->updateCache();
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page, $where="")
	{
		$totalNum = $this->db->fetchOne ('select count(*) from sfdata ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select t1.*,t2.username as adminame from sfdata t1
        left join admin t2 on t2.id=t1.adminid
        ' . $where .' order by id desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}

	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchDailiAll($adminid, $nowp, $per_page, $where="")
	{
		$where .= ' and adminid=' . $adminid;
		$totalNum = $this->db->fetchOne ('select count(*) from sfdata ' . $where );
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select t1.*,t2.username as adminame from sfdata t1
        left join admin t2 on t2.id=t1.adminid
        ' . $where .' order by id desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	private function updateCache()
	{
		FileCache::getInstance()->deleteTag("sfdata");
	}
	/**
	 * 显示
	 */
	public function show($id)
	{
		return $this->db->fetchRow("select * from `sfdata` where id=\"?\"", array($id));
	}
	
	/**
	 * 获取当前显示列表
	 *
	 * @return unknown
	 */
	public function getCurrentList()
	{
		$st = microtime(true);
		$today = date("Y-m-d 00:00:00");
		
		$hour = date("H");// 当前小时
		$minute = date("i") >= 30 ? 30 : '00';// 当前天
		
		$current_date = date("Y-m-d H:$minute:00");		
		$current_day = date("d", strtotime($today));
		$mingtian_day = date("d", strtotime("+1 day"));
		$houtian_day = date("d", strtotime("+2 day"));
		$houtian_date = date("Y-m-d 00:00:00", strtotime("+2 day"));
		
		$key = md5('sf_current_list_' . $today);
		if (($list = FileCache::getInstance()->get($key)) == null){
			$list = $this->db->fetchAll("select * from sfdata force index(getCurrentList) where ischeck=1 and sdatetime>='" . $today . "' order by sdatetime asc, id asc");
			FileCache::getInstance()->set($key, $list, 0, array("sfdata"));
		}
		
		$result = array();
		// 白天档
		if ($hour >= 7){			
			// for debug
			/**
			$today = "2011-11-26 00:00:00";
			$current_date = "2011-11-26 11:00:00";
			$minute = '00';
			**/
		
			$arr_best = array();
			$arr_current_hour = array();
			$arr_isfine = array();
			$arr_past = array();
			$arr_next = array();
			$arr_txgd = array();
			$arr_txtj = array();
			$arr_mt_gd = array();
			$arr_mt_bt = array();
			$arr_mt_tx = array();
			$arr_ht = array();
			
			// 全天固顶
			foreach ($list as $key => $v){
				$sdatetime = strtotime($v['sdatetime']);
				$sdatetime_d = date("d", $sdatetime);
				
				if ($v['isbest'] && $sdatetime_d == $current_day){
					$v['type'] = 'isbest';
					$arr_best[] = $v;
					continue;
				}
								
				// 当前半小时内的开服广告
				if ($v['sdatetime'] == $current_date && $v['isfinehot'] ==0 && $v['ishot'] == 0 && $v['isdayfine'] == 0){
					$v['type'] = 'current_hour';
					$arr_current_hour[] = $v;
					continue;
				}

				// 精品全天推荐信息	
				if ($v['isdayfine'] && $sdatetime_d == $current_day){
					$v['type'] = 'isdayfine';
					$arr_isfine[] = $v;
					continue;
				}
				
				//当天已过去时间档信息
				if ($v['sdatetime'] < $current_date && $v['isfinehot'] ==0 && $v['ishot'] == 0){
					$v['type'] = 'past';
					$arr_past[] = $v;
					continue;
				}
				// 当天已过去时间，为倒序排列
				if(!empty($arr_past)){
					// 取得列的列表
					foreach ($arr_past as $key => $row) {
					    $volume[$key]  = $row['sdatetime'];
					}
					
					// 将数据根据 volume 降序排列
					// 把 $data 作为最后一个参数，以通用键排序
					array_multisort($volume, SORT_DESC, $arr_past);
				}
				
				// 当天未到时间档信息
				if ($v['sdatetime'] > $current_date && $sdatetime_d == $current_day && $v['isfinehot'] ==0 && $v['ishot'] == 0){
					$v['type'] = 'next';
					$arr_next[] = $v;
					continue;
				}
				
				// 当天的通宵固顶信息 相当于明天早晨的
				if ($v['isfinehot'] && $sdatetime_d == $mingtian_day){
					$arr_txgd[] = $v;
					continue;
				}
				//当天通宵推荐信息 相当于明天早晨的
				if ($v['ishot'] && $sdatetime_d == $mingtian_day){
					$arr_txtj[] = $v;
					continue;
				}
				//明天全天固顶信息
				if ($v['isbest'] && $sdatetime_d == $mingtian_day){
					$arr_mt_gd[] = $v;
					continue;
				}
				
				// 明天的从早7点开始的顺序排列信息
				if ($sdatetime_d == $mingtian_day 
				&& date("H", $sdatetime) >= 7){
					$v['type'] = 'mt_bt';
					$arr_mt_bt[] = $v;
					continue;
				}
				
				// 明天的通宵固顶及通宵推荐信息 相当于后天 早晨的
				if ( ($v['isfinehot'] || $v['ishot']) 
				&& $sdatetime_d == $houtian_day){
					$arr_mt_tx[] = $v;
					continue;
				}
				// 后天及后天以后的信息按顺序排列
				if ( $v['sdatetime'] >= $houtian_date){
					$v['type'] = 'ht';
					$arr_ht[] = $v;
					continue;
				}
			}
			
			// 半小时内部排序 分为6个次序，轮到则排最前面
			$count = count($arr_current_hour);
			if ($count >= 6){
				$t = date("i");
				$seq = (int)($t / 5);
		        if ($seq > 5)
		            $seq -= 6;
		        $r = array_splice($arr_current_hour, $count / 6 * $seq, $count / 6);
		        $arr_current_hour = array_merge($r, $arr_current_hour);
			}
			
			$result = array_merge($arr_best, $arr_current_hour, $arr_isfine,$arr_past
			,$arr_next, $arr_txgd,$arr_txtj, $arr_mt_gd, $arr_mt_bt, $arr_mt_tx, $arr_ht);
		}else{// 通宵档
			$arr_best = array();
			$arr_txgd = array();
			$arr_txtj = array();
			$arr_jt_bt = array();
			$arr_mt_gd = array();
			$arr_mt_bt = array();
			$arr_mt_tx = array();
			$arr_ht = array();
			
			
			// 全天固顶
			foreach ($list as $key => $v){
				$sdatetime = strtotime($v['sdatetime']);
				$sdatetime_d = date("d", $sdatetime);
				
				if ($v['isbest'] && $sdatetime_d == $current_day){
					$arr_best[] = $v;
					continue;
				}
				
				// 当天的通宵固顶信息 这里是今天的
				if ($v['isfinehot'] && $sdatetime_d == $current_day){
					$arr_txgd[] = $v;
					continue;
				}
				//当天通宵推荐信息 这里是今天的
				if ($v['ishot'] && $sdatetime_d == $current_day){
					$arr_txtj[] = $v;
					continue;
				}
				// 今天的从早7点开始的顺序排列信息
				if ($sdatetime_d == $current_day 
				&& date("H", $sdatetime) >= 7){
					$arr_jt_bt[] = $v;
					continue;
				}
				//明天全天固顶信息
				if ($v['isbest'] && $sdatetime_d == $mingtian_day){
					$arr_mt_gd[] = $v;
					continue;
				}
				// 明天的从早7点开始的顺序排列信息
				if ($sdatetime_d == $mingtian_day 
				&& date("H", $sdatetime) >= 7){
					$v['type'] = 'mt_bt';
					$arr_mt_bt[] = $v;
					continue;
				}
				// 明天的通宵固顶及通宵推荐信息 相当于后天 早晨的
				if ( ($v['isfinehot'] || $v['ishot']) 
				&& $sdatetime_d == $mingtian_day){
					$arr_mt_tx[] = $v;
					continue;
				}
				
				// 后天及后天以后的信息按顺序排列
				if ( $sdatetime_d >= $houtian_date){
					$v['type'] = 'ht';
					$arr_ht[] = $v;
					continue;
				}
			}
			
			$result = array_merge($arr_best, $arr_txgd, $arr_txtj, $arr_jt_bt, $arr_mt_gd, $arr_mt_bt, $arr_mt_tx, $arr_ht);
		}
		
		return $result;
	}
}